# -*- coding: utf-8 -*- 
# cpp_writer.py
# write cpp file

import os

CPP_FILE = 'code/datatable.cpp'
CPP_CODE_IMPORT = '#include "datatable.h"\n\n'

CPP_CLASS_FUN = 'void %s::readline(std::stringstream& _stream)\n{\n\t_stream %s;\n}\n'
CPP_TABLE_FUN = 'void %sTable::readline(std::stringstream& _stream)\n{\n\t%s* data = new %s();\n\tdata->readline(_stream);\n\t_%sList.insert(std::make_pair(data->key, data));\n}\n\n'

class cppfilewriter:

    def write_code(self,code):
        file_stream = open(CPP_FILE,'w')
        file_stream.write(code)
        file_stream.close()
        
    def add_code(self,code):
        file_stream = open(CPP_FILE,'a')
        file_stream.write(code)
        file_stream.close()
    
    def write_import(self):
        self.write_code(CPP_CODE_IMPORT)

    def add_end(self):
        self.add_code(CODE_END)
    
    def add_class_func(self,tablename,format_list):
        var = ''
        for i in range(len(format_list)):
            var += '\n\t\t>> '
            var += format_list[i]
        self.add_code(CPP_CLASS_FUN%(tablename,var))
            
    def add_table_func(self,tablename):
        self.add_code(CPP_TABLE_FUN%(tablename,tablename,tablename,tablename,))
